package com.teradata.dashboard.ui.fragment;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.teradata.dashboard.R;
import com.teradata.dashboard.ui.activity.MyComments;
import com.teradata.dashboard.ui.activity.PreferrnceActivity;
import com.teradata.dashboard.utils.HttpUtil;

public class ProfileFragment extends Fragment {
	private  ImageView userImg;
	private  ImageView userImgStrench;
	private String userName;
    JSONObject jsonObj;
    JSONObject data;
   
    
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		View profileView = inflater.inflate(R.layout.profile, container,false);
		RelativeLayout profile_setting_l=(RelativeLayout)profileView.findViewById(R.id.profile_setting_l);
		profile_setting_l.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){
				Intent intent=new Intent(getActivity(),PreferrnceActivity.class);				
				Bundle bundle = new Bundle();
	            bundle.putString("USERNAME", userName);
	            intent.putExtra("bundle", bundle);
				startActivity(intent);
			} 
		});		
		
		//查询用户信息
		TextView userName = (TextView) getActivity().findViewById(R.id.profile_user_name);
        
		try {
			jsonObj = queryUser();
			System.out.println(jsonObj.getString("userName"));
			userName.setText(jsonObj.getString("userName"));
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}	
				
		RelativeLayout profile_mycomment=null;
		profile_mycomment=(RelativeLayout)profileView.findViewById(R.id.profile_mycomment);
		profile_mycomment.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){
				Intent intent=new Intent(getActivity(), MyComments.class);
				startActivity(intent);
			} 
		});		
		return profileView;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
		getActivity().getActionBar().setDisplayShowHomeEnabled(false);
		getActivity().getActionBar().setHomeButtonEnabled(true);
		getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		getActivity().getActionBar().setTitle(R.string.setting);		
	}
	
	private void initHead() {
		final ActionBar actionBar = getActivity().getActionBar(); 
		actionBar.setDisplayShowTitleEnabled(true);
		actionBar.setDisplayShowCustomEnabled(true);
		View actionBarButtons = LayoutInflater.from(getActivity()).inflate(R.layout.head,
                new LinearLayout(getActivity()), false);
		((TextView)actionBarButtons.findViewById(R.id.tv_head)).setText(R.string.profile);
		actionBarButtons.setOnClickListener(new OnClickListener() {//监听事件  
            @Override
			public void onClick(View arg0) {  
                switch (arg0.getId()) {  
                case R.id.btn_leftTop:  
                    break;  
                default:  
                    break;  
                }  
            }  
        });  
		this.getActivity().getActionBar().setCustomView(actionBarButtons);
	}
	
		
	private JSONObject queryUser()
			throws Exception
		{
			//使用Map封装请求参数
			Map<String, String> map = new HashMap<String, String>();
			// 定义发送请求的URL
			String url = HttpUtil.BASE_URL + "tpifauth/findUser.action";
			// 发送请求
			return new JSONObject(HttpUtil.postRequest(url, map));
		}
	
	@Override  
    public void onActivityCreated(Bundle savedInstanceState) {  
         
		 super.onActivityCreated(savedInstanceState);
            
         Typeface iconfont = Typeface.createFromAsset(getActivity().getAssets(), "fonts/fontawesome-webfont.ttf");
   		 ((TextView)getView().findViewById(R.id.profile_user_info_i)).setTypeface(iconfont);
   		 ((TextView)getView().findViewById(R.id.profile_comments_i)).setTypeface(iconfont);
   		 ((TextView)getView().findViewById(R.id.profile_messages_i)).setTypeface(iconfont);
   		 ((TextView)getView().findViewById(R.id.profile_setting_i)).setTypeface(iconfont);
   		 
	}
}
